<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2020-08-08
 * Time: 下午 04:08
 */
namespace Cmd;
use Cmd\Base\User;
use Lib\Cmd;
use Model\User as UserModel;

class Userlist extends User {
    protected $_options = ['hash'];
    
    public function main() {
        $data = $this->userModel->getAll();
        $all = array_keys($data);
        $maxLength = 0;
        
        $max = function ($row) use(&$maxLength) {
            $len = strlen($row);
            $maxLength = $len > $maxLength ? $len : $maxLength;
            return $row;
        };
        
        array_map($max, $all);
        
        if($this->existParam('hash')) {
            $all = array_map(function ($row) use($data, $maxLength) {
                return str_pad($row, $maxLength+1, ' ') . $data[$row]['hash_method'];
            }, $all);
        }
        
        $this->writeInfo(implode(PHP_EOL, $all));
    }
    
    public function help() {
        $this->writeInfo("功能: 查看用户列表");
        $this->writeInfo("用法: ./exec userlist [--hash]");
        $this->writeLn();
        $this->writeInfo("options:");
        $this->writeLn();
        $this->writeInfo("  --hash 显示用户的hash方式");
    }
}